

#include "stdafx.h"
#include "pf_leader.h"

#include "pf_debug.h"
#include "pf_line.h"
#include "pf_solid.h"


/**
 * Constructor.
 */
PF_Leader::PF_Leader(PF_Container* parent)
        :PF_Container(parent) {

    empty = true;
}


/**
 * Constructor.
 * @param d Leader data
 */
PF_Leader::PF_Leader(PF_Container* parent,
                     const PF_LeaderAttribute& d)
        :PF_Container(parent), data(d) {
    empty = true;
}



/**
 * Destructor 
 */
PF_Leader::~PF_Leader() {}



/**
 * Implementation of update. Updates the arrow.
 */
void PF_Leader::update() {

    // find and delete arrow:
    for (PF_Entity* e=firstEntity(); e!=NULL; e=nextEntity()) {
        if (e->rtti()==LX_Define::EntitySolid) {
            removeEntity(e);
            break;
        }
    }
	
	if (isUndone()) {
		setVisible(false);
		return;
	}

    PF_Entity* fe = firstEntity();
    if (fe!=NULL && fe->isAtomic()) {
        PF_Vector p1 = ((PF_AtomicEntity*)fe)->getStartpoint();
        PF_Vector p2 = ((PF_AtomicEntity*)fe)->getEndpoint();

        // first entity must be the line which gets the arrow:
        if (hasArrowHead()) {
            PF_Solid* s = new PF_Solid(this, PF_SolidAttribute());
            s->shapeArrow(p1,
                          p2.angleTo(p1),
                          getGraphicVariableDouble("$DIMASZ", 2.5));
            s->setPen(PF_Pen(LX_Define::FlagInvalid));
            s->setLayer(NULL);
            PF_Container::addEntity(s);
        }
    }
}



/**
 * Adds a vertex from the endpoint of the last element or 
 * sets the startpoint to the point 'v'.
 *
 * The very first vertex added is the starting point.
 * 
 * @param v vertex coordinate
 *
 * @return Pointer to the entity that was addded or NULL if this
 *         was the first vertex added.
 */
PF_Entity* PF_Leader::addVertex(const PF_Vector& v) {

    PF_Entity* entity=NULL;
    static PF_Vector last = PF_Vector(false);

    if (empty) {
        last = v;
        empty = false;
    } else {
        // add line to the leader:
        entity = new PF_Line(this, PF_LineAttribute(last, v));
        entity->setPen(PF_Pen(LX_Define::FlagInvalid));
        entity->setLayer(NULL);
        PF_Container::addEntity(entity);

		if (count()==1 && hasArrowHead()) {
			update();
		}

        last = v;
    }

    return entity;
}



/**
 * Reimplementation of the addEntity method for a normal container.
 * This reimplementation deletes the given entity!
 *
 * To add entities use addVertex() instead.
 */
void PF_Leader::addEntity(PF_Entity* entity) {
    PF_DEBUG->print(PF_Debug::D_WARNING, "PF_Leader::addEntity:"
                    " should never be called");

    if (entity==NULL) {
        return;
    }
    delete entity;
}



void PF_Leader::move(PF_Vector offset) {
    PF_Container::move(offset);
    update();
}



void PF_Leader::rotate(PF_Vector center, double angle) {
    PF_Container::rotate(center, angle);
    update();
}



void PF_Leader::scale(PF_Vector center, PF_Vector factor) {
    PF_Container::scale(center, factor);
    update();
}



void PF_Leader::mirror(PF_Vector axisPoint1, PF_Vector axisPoint2) {
    PF_Container::mirror(axisPoint1, axisPoint2);
    update();
}


void PF_Leader::stretch(PF_Vector firstCorner,
                       PF_Vector secondCorner,
                       PF_Vector offset) {

    PF_Container::stretch(firstCorner, secondCorner, offset);
    update();
}

/**
 * Dumps the leader's data to stdout.
 */
std::ostream& operator << (std::ostream& os, const PF_Leader& l) {
    os << " Leader: " << l.getData() << " {\n";

    os << (PF_Container&)l;

    os << "\n}\n";

    return os;
}

